package com.gopay.checkorder.dao.impl;

import com.gopay.checkorder.dao.BisTimingFuseDao;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.bank.bcdl.BisTimingFuse;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 银行定时熔断
 *
 * @author boai
 * @date 2018-02-08
 */
@Repository("bisTimingFuseDao")
public class BisTimingFuseDaoImpl extends BaseDAOImpl<BisTimingFuse,Long> implements BisTimingFuseDao {

    @Override
    public List<BisTimingFuse> findBisTimingFuseAll() {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(BisTimingFuse.class);
        detachedCriteria.add(Restrictions.in("timingStatus",new Object[]{BisTimingFuse.TimingStatus.UNEXECUTED.getCode(), BisTimingFuse.TimingStatus.EXECUTING.getCode()}));
        detachedCriteria.add(Restrictions.eq("deleteFlag", BisTimingFuse.DeleteFlag.UNDELETE.getCode()));
        detachedCriteria.addOrder(Order.desc("startFusingTs"));
        List<BisTimingFuse> list = findByCriteria(detachedCriteria);
        return list;
    }

    @Override
    public BisTimingFuse findBisTimingFuseByExe(String sentinelCode) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(BisTimingFuse.class);
        detachedCriteria.add(Restrictions.in("timingStatus",new Object[]{BisTimingFuse.TimingStatus.UNEXECUTED.getCode(), BisTimingFuse.TimingStatus.EXECUTING.getCode()}));
        detachedCriteria.add(Restrictions.eq("deleteFlag", BisTimingFuse.DeleteFlag.UNDELETE.getCode()));
        detachedCriteria.addOrder(Order.desc("startFusingTs"));
        List<BisTimingFuse> list = findByCriteria(detachedCriteria);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }
}
